﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace ZenInterface
{



    public partial class ZenForm : Form
    {

        #region variables
        zenHardware zenHardwareForm;
        serialPortControllerClass serialPortController;
        dataParserClass dataParserController;
        digitalFilterClass digitalFilter;
        RouterControllerClass routerControl;
        spectralGraphClass spectralGraph;
        temporalGraphClass rawGraph;
        temporalGraphClass filteredGraph;

        zenStatusClass zenStatus;
        public Properties.Settings settings;        

        #endregion

        #region initialize
        public ZenForm()
        {
            InitializeComponent();
            //attach settings
            settings = new ZenInterface.Properties.Settings();
            //set up tables
            setupDataTables();

            zenStatus = new zenStatusClass();
            
            //forms
            zenHardwareForm = new zenHardware(settings, zenStatus);
            serialPortController = new serialPortControllerClass(settings);
            dataParserController = new dataParserClass(settings, databaseZenDataSet.Tables["DataTableChannels"], zenStatus);
            digitalFilter = new digitalFilterClass(settings, databaseZenDataSet);
            routerControl = new RouterControllerClass();
            spectralGraph = new spectralGraphClass(settings);
            rawGraph = new temporalGraphClass(settings);
            filteredGraph = new temporalGraphClass(settings);
            zenStatus = new zenStatusClass();

            //set up the display timer
            timerUpdate.Interval = settings.userDisplayUpdateRate;
            timerUpdate.Enabled = true;
            timerUpdate.Start();


            //set up the serial port
            timerSerial.Interval = settings.userSerialBufferUpdateRate;
            timerSerial.Enabled = false;
            

            //set up the graphs
            //raw graph
            
            //filter graph
            //spectral graph

            //set up the digital filter
            //eventLog.Log = DateTime.Now.ToLongTimeString();
            //eventLog.WriteEntry("Program Started");

            
           
            

        }
        #endregion

        #region events
        #region settings
        private void serialPortPictureBox_Click(object sender, EventArgs e)
        {
            if (serialPortController.IsDisposed)
            {
                serialPortController = new serialPortControllerClass(settings);
            }
            //show serial port form
            if (serialPortController.Visible)
            {
                serialPortController.Hide();
            }
            else
            {
                serialPortController.Show();
            }

        }
        private void pictureBoxParser_Click(object sender, EventArgs e)
        {
            //show parser form
            if (dataParserController.IsDisposed)
            {
                //
                dataParserController = new dataParserClass(settings, databaseZenDataSet.Tables["DataTableChannels"], zenStatus);
            }
            if (dataParserController.Visible)
            {
                dataParserController.Hide();
            }
            else
            {
                dataParserController.Show();
            }
        }
        private void pictureBoxDigitalFilter_Click(object sender, EventArgs e)
        {
            if (digitalFilter.IsDisposed)
            {
                digitalFilter = new digitalFilterClass(settings, databaseZenDataSet);
            }
            //show digital filter form
            if (digitalFilter.Visible)
            {
                digitalFilter.Hide();
            }
            else
            {
                digitalFilter.Show();
            }
        }
        private void pictureBoxRouter_Click(object sender, EventArgs e)
        {
            if (routerControl.IsDisposed)
            {
                routerControl = new RouterControllerClass();
            }
            //show router form
            if (routerControl.Visible)
            {
                routerControl.Hide();
            }
            else
            {
                routerControl.Show();
            }
        }
        #endregion
        #region graphs
        private void pictureBoxFilterGraph_Click(object sender, EventArgs e)
        {
            //show filter graph
            if (filteredGraph.IsDisposed)
            {
                filteredGraph = new temporalGraphClass(settings);
            }

            if (filteredGraph.Visible)
            {
                filteredGraph.Hide();
            }
            else
            {
                filteredGraph.Show();
            }
        }
        private void pictureBoxSpectralGraph_Click(object sender, EventArgs e)
        {
            if (spectralGraph.IsDisposed)
            {
                spectralGraph = new spectralGraphClass(settings);
            }
            //show spectral graph
            if (spectralGraph.Visible)
            {
                spectralGraph.Hide();
            }
            else
            {
                spectralGraph.Show();
            }
        }
        private void pictureBoxRawGraph_Click(object sender, EventArgs e)
        {
            //show raw graph
            if (rawGraph.IsDisposed)
            {
                rawGraph = new temporalGraphClass(settings);
            }
            if (rawGraph.Visible)
            {
                rawGraph.Hide();
            }
            else
            {
                rawGraph.Show();
            }
        }
        #endregion
        #region zen hardware
        private void pictureBoxZenBoard_Click(object sender, EventArgs e)
        {
            if (zenHardwareForm.IsDisposed)
            {
                zenHardwareForm = new zenHardware(settings, zenStatus);
            }
            //show zen hardware
            if (zenHardwareForm.Visible)
            {
                zenHardwareForm.Hide();
            }
            else
            {
                zenHardwareForm.Show();
            }
        }
        #endregion
        #endregion
        #region functions
        private void setupDataTables()
        {
            int i = 0;
            DataRow dataRow;
            Random random = new Random();

            databaseZenDataSet.DataTableChannels.Clear();

            i = databaseZenDataSet.DataTableChannels.Rows.Count;
            for (; i < settings.applicationDataBufferSize; i++)
            {
                dataRow = databaseZenDataSet.DataTableChannels.Rows.Add();
                //dataRow[0] = random.Next(0, 100);
                //databaseZenDataSet.Tables["DataTableChannels"0.Rows["channel0"] = (Int32)0;
            }

            i = 0;
            for (; i < settings.applicationDataBufferSize; i++)
            {
                dataRow = databaseZenDataSet.DataTableDigitalFilter0.Rows.Add();
                dataRow[1] = (double)(((int)dataRow[0]) * (settings.userSerialUpdateRate / settings.applicationDataBufferSize));
                dataRow[2] = 1;
                dataRow[3] = 0;

                dataRow = databaseZenDataSet.DataTableDigitalFilter1.Rows.Add();
                dataRow[1] = (double)(((int)dataRow[0]) * (settings.userSerialUpdateRate / settings.applicationDataBufferSize));
                dataRow[2] = 1;
                dataRow[3] = 0;

                dataRow = databaseZenDataSet.DataTableDigitalFilter2.Rows.Add();
                dataRow[1] = (double)(((int)dataRow[0]) * (settings.userSerialUpdateRate / settings.applicationDataBufferSize));
                dataRow[2] = 1;
                dataRow[3] = 0;

                dataRow = databaseZenDataSet.DataTableDigitalFilter3.Rows.Add();
                dataRow[1] = (double)(((int)dataRow[0]) * (settings.userSerialUpdateRate / settings.applicationDataBufferSize));
                dataRow[2] = 1;
                dataRow[3] = 0;
            }

            databaseZenDataSet.DataTableFilterChannels.Clear();

            i = databaseZenDataSet.DataTableFilterChannels.Rows.Count;
            for (; i < settings.applicationDataBufferSize; i++)
            {
                dataRow = databaseZenDataSet.DataTableFilterChannels.Rows.Add();
            }
        }
        private void update()
        {
            int moduleState = 0;
            //update all the pictures
            moduleState = serialPortController.update();
            //serialPort
            switch (moduleState)
            {
                case ((int)serialState.NotConnected):
                    serialPortPictureBox.Image = Properties.Resources.notConnectSerial;
                    break;
                case ((int)serialState.CantFindDevice):
                    serialPortPictureBox.Image = Properties.Resources.notConnectSerial;
                    break;
                case ((int)serialState.Error):
                    serialPortPictureBox.Image = Properties.Resources.notConnectSerial;
                    break;
                case ((int)serialState.Connecting):
                    serialPortPictureBox.Image = Properties.Resources.connectingSerial;
                    break;
                case ((int)serialState.Scanning):
                    serialPortPictureBox.Image = Properties.Resources.connectingSerial;
                    break;
                case ((int)serialState.Connected):
                    serialPortPictureBox.Image = Properties.Resources.connectSerial;
                    break;
                case ((int)serialState.Ready):
                    serialPortPictureBox.Image = Properties.Resources.connectSerial;
                    break;
                default:
                    serialPortPictureBox.Image = Properties.Resources.notConnectSerial;
                    break;
            }



            //data parser
            if (settings.userSerialConnected)
            {
                timerSerial.Enabled = true;
                timerSerial.Start();
                pictureBoxParser.Image = Properties.Resources.parsingData;
                pictureBoxRawGraph.Image = Properties.Resources.waves;
                pictureBoxFilterGraph.Image = Properties.Resources.waves;
                pictureBoxDigitalFilter.Image = Properties.Resources.FilterData;
                pictureBoxSpectralGraph.Image = Properties.Resources.spectrumData;
            }
            else
            {
                timerSerial.Enabled = false;
                pictureBoxParser.Image = Properties.Resources.notParsingData;
                timerSerial.Stop();
                pictureBoxRawGraph.Image = Properties.Resources.noWaves;
                pictureBoxFilterGraph.Image = Properties.Resources.noWaves;
                pictureBoxDigitalFilter.Image = Properties.Resources.FilterNoData;
                pictureBoxSpectralGraph.Image = Properties.Resources.spectrumNoData;
            }

            //data parser
            //digital filter
            moduleState = digitalFilter.update();
            labelDigitalFilter.Text = digitalFilter.stateTranslate(digitalFilter.update());
            //raw image
            if (!rawGraph.IsDisposed)
            {
                rawGraph.update(databaseZenDataSet.DataTableChannels);
            }
            //spectral Graph
            if (!spectralGraph.IsDisposed)
            {
                spectralGraph.update(databaseZenDataSet);
            }
            //filterd image
            if (!filteredGraph.IsDisposed)
            {
                filteredGraph.update(databaseZenDataSet.DataTableFilterChannels);
            }

            //zen hardware
            labelZenHardwareStatus.Text = zenHardwareForm.stateTranslate(zenHardwareForm.update());
            labelSerialPortStatus.Text = serialPortController.stateTranslate(serialPortController.update());


            
            
        }
        #endregion
        #region interrupts
        private void timerUpdate_Tick(object sender, EventArgs e)
        {
            //every time this fires off we update the display
            update();
        }
        private void timerSerial_Tick(object sender, EventArgs e)
        {
            //get the buffered data from the serial port to the parser
            if (dataParserController.parseIncommingData(serialPortController.getInputData()))
            {
                //if we receive a true, we are ready to send the data off to the filter, and the graph
                digitalFilter.applyDigitalFilter();
            }
        }
        #endregion
        
    }
}
